package com.cgtong.android.mvp.presenter;

import com.cgtong.android.bean.UserBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.http.callback.FollowCallBack;
import com.cgtong.android.mvp.contract.UserFollowedContract;
import com.cgtong.android.mvp.model.UserFollowedModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import java.util.List;

/**
 * Created by chunc on 2017/10/26.
 */

public class UserFollowedPresenter implements UserFollowedContract.Presenter {


    private UserFollowedContract.Model mModle;
    private UserFollowedContract.View mView;

    public UserFollowedPresenter(UserFollowedContract.View view) {
        this.mView = view;
        mModle = new UserFollowedModel();
    }

    @Override
    public void httpUserFollowedData(long maxId,String keyValue, final boolean isRefresh) {
        if (isRefresh) {
            mView.setEnableLoadMore(false);
//            mView.setRefreshing(true);
        }
        mModle.httpUserollowedData(maxId,  keyValue,new BaseCallBack<List<UserBean>>() {
            @Override
            public void onSuccess(List<UserBean> problemBeen) {
                if (mView != null) {
                    if (isRefresh) {
                        mView.setRefreshing(false);
                        mView.setEnableLoadMore(true);
                        if (problemBeen == null || problemBeen.size() == 0) {
                            mView.onNullData();
                            return;
                        } else if (problemBeen.size() < ConstantManager.USER_FOLLOWED_PAGE_SIZE_INT) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                        mView.onHttpUserFollowedDataSuccess(problemBeen, isRefresh);
                    } else {
                        mView.onHttpUserFollowedDataSuccess(problemBeen, isRefresh);
                        if (problemBeen.size() < ConstantManager.USER_FOLLOWED_PAGE_SIZE_INT) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                    }
                }
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.setRefreshing(false);
                    mView.onError(e);
                }
            }
        });
    }


    @Override
    public void onDestroy() {
        if (mView != null)
            mView = null;
    }

    @Override
    public void follow(long id, final int position) {
        mModle.follow(id, new FollowCallBack<ApiResult>() {
            @Override
            public void onFollowUserSuccess(long id, ApiResult apiResult) {
                if (mView != null)
                    mView.onFollowSuccess(id,position, apiResult);
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null)
                    mView.onError(e);
            }

        });
    }
}
